﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Agro.Library;
using Ninject;
using Agro.Services;

namespace Agro.Web.Controllers
{
    public class LoginController : Controller
    {
        [Inject]
        public ILogonService LogonService { get; set; }

        [OutputCache(Duration = 600)]
        public ActionResult Index()
        {
            //string BrowserVersion = getBrowser();
            //ViewBag.BrowserVersion = BrowserVersion.ToUpper();
            //HttpCookie cookie = Request.Cookies["ArgoUserInfo"];
            //if (cookie != null)
            //    ViewBag.userName = cookie.Values["userName"];
            //string url = "http://cyhtj.haagri.gov.cn";
            //Response.Redirect(url);


            return View();
        }

        [HttpPost]
        public ActionResult ValidateUser(string userName, string password,string type, string validateCode)
        {
            string msg = string.Empty;
            if (validateCode != Session["ValidateCode"].ToString())
            {
                msg = "验证码不正确";
            }
            else
            {
                if (!LogonService.UserLogon(userName, password,type, out msg))
                {
                   
                }
                else
                {
                    //TODO: 屏蔽更新SID
                    //workFlowInstance.UpdateUserSID(userInfo.UserName, userInfo.SID);

                    HttpCookie cookie = new HttpCookie("ArgoUserInfo");
                    cookie.Expires = DateTime.Now.AddMonths(1);
                    cookie.Values.Add("userName", userName);
                    //if (!string.IsNullOrEmpty(financeDate))
                    //{
                    //    cookie.Values.Add("financeDate", financeDate);
                    //}
                    Response.Cookies.Add(cookie);
                }
            }
            return Content(msg);
        }

        public ActionResult GetValidateCode()
        {
            ValidateCode vCode = new ValidateCode();
            string code = vCode.CreateValidateCode(4);
            Session["ValidateCode"] = code;
            byte[] bytes = vCode.CreateValidateGraphic(code);
            return File(bytes, @"image/jpeg");
        }
        public ActionResult ReLogin()
        {
            return View();
        }
        public ActionResult LogOff()
        {
            LogonService.Logout();
            return RedirectToAction("Login", "Index");
        }

        //获取浏览器版本号
        public static string getBrowser()
        {
            string browsers;
            HttpBrowserCapabilities bc = System.Web.HttpContext.Current.Request.Browser;
            string aa = bc.Browser.ToString();
            string bb = bc.Version.ToString();
            browsers = (aa+bb).ToUpper();
            return browsers;
        }
    }
}
